/**
 * @author Tobias Boehm
 *
 * 03.05.2011
 */
package org.anddev.andengine.extension.tbsengine.menu.view;

import java.util.List;

import org.anddev.andengine.extension.tbsengine.R;
import org.anddev.andengine.extension.tbsengine.match.playerslot.AbsClientPlayerSlot;
import org.anddev.andengine.extension.tbsengine.shared.IImageOrTextSelectableItem;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This is an abstract dialog which can be used to easily develop selection
 * pop-ups for the player configuration components (Faction, Team etc.) inside
 * your implementation of {@link AbsClientPlayerSlot}. 
 * 
 * @author Tobias Boehm
 *
 * @param <PlayerSlotClass> - Your implementation of {@link AbsClientPlayerSlot}.
 */
public abstract class AbsPrepareMatchChoiceDialog<PlayerSlotClass extends AbsClientPlayerSlot> extends Dialog {

	protected final View mContentView;
	protected final PlayerSlotClass mPlayerSlotRow;
	protected ListView mListView;
	protected final Activity mActivity;
	
	public AbsPrepareMatchChoiceDialog(final Activity pActivity, final List<IImageOrTextSelectableItem> objects, final PlayerSlotClass pPlayerSlotRow) {
		super(pActivity);

		this.mActivity = pActivity;
		
		this.mPlayerSlotRow = pPlayerSlotRow;
		
		this.mContentView = pActivity.getLayoutInflater().inflate(R.layout.choice_dialog, null);
		
		this.setContentView(mContentView);
		
		this.setTitle(this.getDialogTitle());
		
		this.initListView(objects);
	}

	protected abstract CharSequence getDialogTitle();

	protected abstract OnItemClickListener createOnItemClickListener();
	
	private void initListView(final List<IImageOrTextSelectableItem> objects) {
		
		mListView = (ListView) mContentView.findViewById(R.id.CDitemListV);
		
		final ArrayAdapter<IImageOrTextSelectableItem> listAdapter = new ArrayAdapter<IImageOrTextSelectableItem>(mContentView.getContext(), R.id.CDitemListV, objects){
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				
				final IImageOrTextSelectableItem currentItem = this.getItem(position);
				
				return new ImageOrTextListElementRow(mActivity, currentItem.getIcon(), currentItem.getName());
			}
		};
		
		mListView.setAdapter(listAdapter);
		
		mListView.setOnItemClickListener(this.createOnItemClickListener());		
	}
}
